<template>
  <div class="login_container">
    <!-- 登录块 -->
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/logo.png" />
      </div>
      <!-- 表单区域 -->
      <el-form class="login_form"
               :model="loginForm"
               :rules="loginFormRules"
               ref="loginFormRef"
               label-width="0">
        <!-- 用户名 -->
        <el-form-item prop="userId">
          <el-input placeholder="账号"
                    v-model.number="loginForm.userId"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="userPwd">
          <el-input type="password"
                    v-model="loginForm.userPwd"
                    placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-radio-group v-model="loginForm.userType">
            <el-radio label="2">学生</el-radio>
            <el-radio label="3">教师</el-radio>
            <el-radio label="1">管理员</el-radio>
          </el-radio-group>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btns">
          <el-button size="small"
                     @click="login()"
                     type="primary">提交</el-button>
          <el-button size="small"
                     type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      loginForm: {
        userId: '',
        userPwd: '',
        userType: '2'
      },
      loginFormRules: {
        userId: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { type: 'number', message: '账号必须为数字', trigger: 'blur' }
        ],
        userPwd: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
      },
      loginResMsg: {
        "LOGIN_SUCCESS": "登录成功",
        "LOGIN_FAILED_NO_USER": "登录失败,用户不存在",
        "LOGIN_FAILED_ERROR_PWD": "登录失败，密码错误",
        "LOGIN_FAILED_TYPE_ERROR": "登录失败，用户类型错误",
      }
    }
  },
  methods: {
    async login () {
      console.log(this.loginForm);
      this.$refs.loginFormRef.validate(async valid => {
        if (!valid) return this.$message.warning("请输入正确的数据");
        const { data: res } = await this.$http.get("/user/login", {
          params: this.loginForm
        });
        console.log(res);
        if (res.code == 200) {
          this.$message.success(this.loginResMsg[res.msg]);
          window.sessionStorage.setItem("user", this.loginForm);
          this.$router.push({ path: "/adminHome" });
        } else {
          this.$message.error(this.loginResMsg[res.msg]);
        }
      })
    }
  }

}
</script>
<style lang="less" scoped>
// 根节点样式
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}
.login_box {
  width: 450px;
  height: 350px;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eeeeee;
    border-radius: 50%;
    padding: 5px;
    box-shadow: 0 0 2px #dddddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #00eeee;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eeeeee;
    }
  }
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.login_form {
  position: absolute;
  bottom: 0%;
  width: 100%;
  padding: 0 10px;
  box-sizing: border-box;
}
</style>
